package com.ruoyi.info.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.info.domain.CmsApp;
import com.ruoyi.info.domain.CmsSandbox;
import com.ruoyi.info.service.ICmsAppService;
import com.ruoyi.info.service.ICmsSandboxService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.util.security.SandboxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Controller
@RequestMapping("/serve")
public class ServeController {
    private String prefix = "serve/";

    @Autowired
    private ICmsAppService cmsAppService;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ICmsSandboxService cmsSandboxService;

    @GetMapping("")
    public String index(ModelMap mmap) {
        SysUser currentUser = ShiroUtils.getSysUser();

        mmap.put("currentUser", currentUser);
        return prefix + "index";
    }

    @GetMapping("/appDetails/{id}")
    public String appDetails(@PathVariable("id") Long id, ModelMap mmap) {
        System.out.println("AppDetails: " + id);

        CmsApp app = cmsAppService.selectCmsAppById(id);

        cmsAppService.getRemoteDetails(app);

        mmap.put("app", app);

        app.setUserId(null);

        return prefix + "appDetails";
    }

    @GetMapping("/wxqbDetails")
    public String wxqbDetails(ModelMap mmap) {
        return prefix + "wxqbDetails";
    }

    @GetMapping("/sandboxDetails/{id}")
    public String sandboxDetails(@PathVariable("id") Long id, ModelMap mmap) {

        CmsSandbox sandbox = cmsSandboxService.selectCmsSandboxById(id);
        SandboxResult result =  cmsSandboxService.getSandboxResult(sandbox);
        JSONObject jo = JSON.parseObject(result.getData());
        mmap.put("result", jo);

        SandboxResult tagResult = cmsSandboxService.getSandboxTags(sandbox);
        JSONObject tags = JSON.parseObject(tagResult.getData());

        mmap.put("tags", tags);
        mmap.put("taskId", sandbox.getTaskId());

        return prefix + "sandboxDetails";
    }

    @GetMapping("/sandboxScreenshot/{taskId}")
    @ResponseBody
    public AjaxResult sandboxScreenshot(@PathVariable("taskId") String taskId) {

        SandboxResult screenshotResult = cmsSandboxService.sandboxScreenshot(taskId);
        JSONObject data = JSON.parseObject(screenshotResult.getData());
        return AjaxResult.success(data);
    }

    @GetMapping("/sandboxNetwork/{taskId}")
    @ResponseBody
    public AjaxResult sandboxNetwork(@PathVariable("taskId") String taskId, @RequestParam Map<String, Object> params) {

        SandboxResult result = cmsSandboxService.sandboxNetwork(taskId, params);
        JSONObject data = JSON.parseObject(result.getData());
        return AjaxResult.success(data);
    }

    /**
     * 行为总览
     * @param taskId
     * @param params
     * @return
     */
    @GetMapping("/sandboxBehaviorProcess/{taskId}")
    @ResponseBody
    public AjaxResult sandboxBehaviorProcess(@PathVariable("taskId") String taskId, @RequestParam Map<String, Object> params) {

        SandboxResult result = cmsSandboxService.sandboxBehaviorProcess(taskId, params);
        JSONObject data = JSON.parseObject(result.getData());
        return AjaxResult.success(data);
    }

    /**
     * 行为总览
     * @param taskId
     * @param params
     * @return
     */
    @GetMapping("/sandboxBehaviorEventsByPid/{taskId}/{pid}/{eventType}")
    @ResponseBody
    public AjaxResult sandboxBehaviorEventsByPid(@PathVariable("taskId") String taskId, @PathVariable("pid") String pid,
                                                 @PathVariable("eventType") String eventType, @RequestParam Map<String, Object> params) {

        SandboxResult result = cmsSandboxService.sandboxBehaviorEventsByPid(taskId, pid, eventType, params);
        JSONObject data = JSON.parseObject(result.getData());
        return AjaxResult.success(data);
    }

    /**
     * 静态信息
     * @param taskId
     * @param params
     * @return
     */
    @GetMapping("/sandboxReportStatic/{taskId}")
    @ResponseBody
    public AjaxResult sandboxReportStatic(@PathVariable("taskId") String taskId, @RequestParam Map<String, Object> params) {

        SandboxResult result = cmsSandboxService.sandboxReportStatic(taskId, params);
        JSONObject data = JSON.parseObject(result.getData());
        return AjaxResult.success(data);
    }



}
